import React from 'react';
import {message} from 'antd';
import axios from 'axios';

export default class {
    saveModelInfo = async () => {
        const [formData, editInfoModel] = this.models.modelInfoM.get(['formData', 'editInfoModel']);
        const list = this.models.modelListM.get('data');
        const modelData = this.models.dataTM.get(`modelDataCollection.${formData.keyName}`);

        if (!formData.name) {
            message.error('填写模型名称');
            return;
        } else if (!formData.keyName) {
            message.error('填写模型字段名称');
            return;
        }

        let rowData = {
            ...formData
        };

        if (editInfoModel || editInfoModel === 0) {
            list[editInfoModel] = rowData;
        } else {
            list.push(rowData);
        }

        this.models.modelListM.set({
            data: [...list]
        });
        const res = await axios.post('/saveModelData', {
            modelData: modelData || {},
            modelInfo: rowData 
        });
        if (res.data.success) {
            this.actions.dmPageA.turnPage('list');
        }
    }

    cancelModelInfo = () => {
        this.actions.dmPageA.turnPage('list');
    }
}